<?php
 /*******************************************************************************
 * @filename: view.ctp
 *
 * @author: immer_baldos
 *
 * @description: orders/view view
 *
 * @history:
 *
 *   VERSION   DATE                   BY          NOTES
 *  ---------------------------------------------------------------
 *     1.0     September  9, 2009     immer       initial bake
 *
 *     2.0     September 27, 2009     immer       modify for PO format
 *
 ******************************************************************************/
 //TODO: format this to PO format
 ?>
<div class="orders view">
<h2><?php  __('Purchase Order');?></h2>
<table style="border:0px;">
    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Order Type'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			__________________&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Delivery Date'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['delivery_date']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Document Type'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			__________________&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Sales Person'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['sales_person']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Order Number'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['order_number']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Created By'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['created_by']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Print Count'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['print_count']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Currency'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo 'PHP';?>
			&nbsp;
		</dd>
    </dl></td></tr>


    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Ordered Date'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['created']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Ship Via'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['ship_via']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Print Date'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['print_date']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
	<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Grand Total'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php
			    $i = 0;
                $sum=0;
		        foreach ($order['Transaction'] as $transaction){
		            $sum+=$transaction['price']*$transaction['count'];
		        }
		        echo number_format($sum,2);
		    ?>
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Cust. Contact'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Customer']['contact']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Sales Terms'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Order']['sales_terms']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Ship To'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Customer']['shipping_address']; ?>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Bill To'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Customer']['billing_address']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __(''); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Credit Limit'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php echo $order['Customer']['credit_limit']; ?>
			&nbsp;
		</dd>
    </dl></td></tr>

    <tr><td style="text-align:left;border:0px;">
	<dl><?php $i = 0; $class = ' class="altrow"';?>
	    <dt<?php if ($i % 2 == 0) echo $class;?>><?php __(''); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			&nbsp;
		</dd>
    </dl></td><td style="text-align:left;border:0px;">
    <dl><?php $i = 0; $class = ' class="altrow"';?>
		<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Credit Rating'); ?></dt>
		<dd<?php if ($i++ % 2 == 0) echo $class;?>>
			<?php //TODO: credit rating computation ?>
			&nbsp;
		</dd>
    </dl></td></tr>

</table>
</div>

<?php if ($_SESSION['Auth']['User']['role'] == "admin"): ?>
<div class="actions">
	<ul>
		<li><?php echo $html->link(__('Edit Order', true), array('action' => 'edit', $order['Order']['id'])); ?> </li>
		<li><?php echo $html->link(__('Delete Order', true), array('action' => 'delete', $order['Order']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $order['Order']['id'])); ?> </li>
		<li><?php echo $html->link(__('List Orders', true), array('action' => 'index')); ?> </li>
		<li><?php echo $html->link(__('New Order', true), array('action' => 'add')); ?> </li>
		<li><?php echo $html->link(__('List Customers', true), array('controller' => 'customers', 'action' => 'index')); ?> </li>
		<li><?php echo $html->link(__('New Customer', true), array('controller' => 'customers', 'action' => 'add')); ?> </li>
		<li><?php echo $html->link(__('List Payments', true), array('controller' => 'payments', 'action' => 'index')); ?> </li>
		<li><?php echo $html->link(__('New Payment', true), array('controller' => 'payments', 'action' => 'add')); ?> </li>
		<li><?php echo $html->link(__('List Transactions', true), array('controller' => 'transactions', 'action' => 'index')); ?> </li>
		<li><?php echo $html->link(__('New Transaction', true), array('controller' => 'transactions', 'action' => 'add')); ?> </li>
	</ul>
</div>
<div class="related">
	<h3><?php __('Related Payments');?></h3>
	<?php if (!empty($order['Payment'])):?>
	<table cellpadding = "0" cellspacing = "0">
	<tr>
		<th><?php __('Id'); ?></th>
		<th><?php __('Amount'); ?></th>
		<th><?php __('Due'); ?></th>
		<th><?php __('Paid'); ?></th>
		<th><?php __('Customer Id'); ?></th>
		<th><?php __('Order Id'); ?></th>
		<th class="actions"><?php __('Actions');?></th>
	</tr>
	<?php
		$i = 0;
		foreach ($order['Payment'] as $payment):
			$class = null;
			if ($i++ % 2 == 0) {
				$class = ' class="altrow"';
			}
		?>
		<tr<?php echo $class;?>>
			<td><?php echo $payment['id'];?></td>
			<td><?php echo $payment['amount'];?></td>
			<td><?php echo $payment['due'];?></td>
			<td><?php echo $payment['paid'];?></td>
			<td><?php echo $payment['customer_id'];?></td>
			<td><?php echo $payment['order_id'];?></td>
			<td class="actions">
				<?php echo $html->link(__('View', true), array('controller' => 'payments', 'action' => 'view', $payment['id'])); ?>
				<?php echo $html->link(__('Edit', true), array('controller' => 'payments', 'action' => 'edit', $payment['id'])); ?>
				<?php echo $html->link(__('Delete', true), array('controller' => 'payments', 'action' => 'delete', $payment['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $payment['id'])); ?>
			</td>
		</tr>
	<?php endforeach; ?>
	</table>
<?php endif; ?>

	<div class="actions">
		<ul>
			<li><?php echo $html->link(__('New Payment', true), array('controller' => 'payments', 'action' => 'add'));?> </li>
		</ul>
	</div>
</div>
<?php endif; ?>
<div class="related">
	<h3><?php __('Order Details');?></h3>
	<?php if (!empty($order['Transaction'])):?>
	<table cellpadding = "0" cellspacing = "0">
	<tr>
		<th><?php __('Product'); ?></th>
		<th><?php __('Description'); ?></th>
		<th><?php __('Price'); ?></th>
		<th><?php __('Count'); ?></th>
		<th><?php __('Total'); ?></th>
	</tr>
	<?php
		$i = 0;
		$sum=0;
		foreach ($order['Transaction'] as $transaction):
			$class = null;
			if ($i++ % 2 == 0) {
				$class = ' class="altrow"';
			}
		?>
		<tr<?php echo $class;?>>
			<?php
			    foreach ($products as $product){
			        if($product['Product']['id'] == $transaction['product_id']){
			            echo '<td>'.$product['Product']['name'].'</td>';
			            echo '<td>'.$product['Product']['description'].'</td>';
			            break 1;
			        }
			    }
			?>
			<td><?php echo $transaction['price'];?></td>
			<td><?php echo $transaction['count'];?></td>
			<td><?php
			        $sum+=$transaction['price']*$transaction['count'];
			        echo number_format($transaction['price']*$transaction['count'],2);
			     ?></td>
		</tr>
	<?php endforeach; ?>
	<td></td><td></td><td></td><td></td><td><?php echo number_format($sum,2); ?></td>
	</table>
<?php endif; ?>
	<div class="actions">
		<ul>
		    <?php if ($_SESSION['Auth']['User']['role'] == "admin"): ?>
			<li><?php echo $html->link(__('New Transaction', true), array('controller' => 'transactions', 'action' => 'add'));?> </li>
            <?php endif; ?>
            <li><?php echo $html->link(__('List Orders', true), array('action' => 'index'));?> </li>
		</ul>
	</div>
</div>
